$(function() {

    var page = 1;
    var pageSize = 5;
    var totalpage;

    $.ajax({
        url: '/category/queryTopCategoryPaging',
        type: 'get',
        data: {
            page: page,
            pageSize: pageSize
        },
        success: function(res) {
            totalpage = Math.ceil(res.total / pageSize);
            // console.log(totalpage);
            var html = template('category', res);
            // console.log(html);
            $('.table').html(html);
        }
    });


    $('#next').on('click', function() {

        page++;
        if (page > totalpage) {
            page = totalpage;
            alert('抱歉客官，给不了你更多');
            return;
        }


        $.ajax({
            url: '/category/queryTopCategoryPaging',
            type: 'get',
            data: {
                page: page,
                pageSize: pageSize
            },
            success: function(res) {
                var html = template('category', res);
                $('.table').html(html);
            }
        });
    });



    $('#prev').on('click', function() {

        page--;
        if (page < 1) {
            page = 1;
            alert('抱歉客官，已经到顶了');
            return;
        }


        $.ajax({
            url: '/category/queryTopCategoryPaging',
            type: 'get',
            data: {
                page: page,
                pageSize: pageSize
            },
            success: function(res) {
                var html = template('category', res);
                $('.table').html(html);
            }
        });
    });



    $('#catebtn').on('click', function() {

        var categoryName = $.trim($("[name='categoryName']").val());
        // console.log(categoryName);


        if (!categoryName) {
            alert('请输入一级分类名称');
            return;
        }
        $.ajax({
            url: '/category/addTopCategory',
            type: 'post',
            data: {
                categoryName: categoryName
            },
            success: function(res) {
                console.log(res);
                if (res.success) {
                    location.reload();
                }
            }
        });

    });


});